package com.ybkj.daijia.api.member.v1;

import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.SecurityUtils;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Feedback;
import com.ybkj.daijia.server.service.FeedbackService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberService;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("memberFeedbackController")
@RequestMapping(value = "member/api/rest/v1")
public class FeedbackController {

    private static final Logger logger = LoggerFactory.getLogger(FeedbackController.class);

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberAccountService memberAccountService;

    /**
     * 反馈
     */
    @RequestMapping(value = "feedback", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport defaultArea(String phone, String content, String appKey, String token,
        String timestamp) {

        logger.debug("recv phone:{},content:{},appKey:{},token:{},timestamp:{}", phone, content,
            appKey, token, timestamp);

        if (StringUtils.isBlank(phone) || StringUtils.isBlank(content) || SecurityUtils
            .isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        if (!StringUtils.isNumeric(timestamp) || !SecurityUtils
            .checkTimestamp(Long.parseLong(timestamp))) {
            return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
        }

        List<String> params = new LinkedList<String>();
        params.add(phone);
        params.add(content);
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        try {
            Passenger passenger = memberService.findOnePassengerByPhone(phone);
            if (null == passenger) {
                return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
            }

            Feedback feedback = new Feedback();
            feedback.setContent(content);
            feedback.setName(passenger.getName());
            feedback.setPhone(phone);
            feedback.setMemberID(passenger.getId());
            feedbackService.insertRecord(feedback);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(feedback);

            return instance;

        } catch (Exception e) {
            logger.error("api v1 feedback error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }
}
